export default {
  data() {
    return {
      orderId: '' ,// 订单id
      res: null, // 订单详情
      loading: true, // 是否正在加载
      // invoiceForm: {
      //   orderId: '',
      //   costPrice: '',
      //   moneyServicePrice: '',
      //   registionPrice: '',
      //   costImg: '',
      //   registionImg: '',
      //   trafficImg: '',
      //   purchaseTaxImg: '',
      //   businessImg: ''
      // },
      invoiceForm: {
        orderId: '',
        clientDrivingImg: '',
        clientInsuranceImg: '',
        clientInvoiceImg: ''
      },
      invoiceImg: '' ,// 发票临时地址
      show: false,
      userId: '',
    }
  },
  onLoad(option) {
    this.orderId = option.id
    this.userId = option.userId
    this.invoiceForm.orderId = option.id
    uni.showLoading({
      title: '加载中'
    })
    this.getShareOrderDetailInfo(option.id)
  },
  methods: {
    // 获取订单详情
    async getShareOrderDetailInfo(id) {
      try{
        let result = await this.$api.order.getShareOrderDetailInfo({
          orderId: id
        })
        result.data.carImg = result.data.carImg ? JSON.parse(result.data.carImg)[0].Img : ''
        result.data.color = result.data.color ? JSON.parse(result.data.color) : {},
        result.data.serviceLookInfo = result.data.serviceLookInfo || {}
        this.res = result.data
        this.userId = res.data.userId
        this.loading = false
        uni.hideLoading()
      }catch(e){
        this.loading = false
        uni.hideLoading()
      }
    },
    // 点击复制订单号
    onClickCopyOrderSN() {
      uni.setClipboardData({
        success: () => {
          data: this.res.orderNo,
          uni.showToast({
            icon: 'none',
            title: '复制成功'
          })
        }
      })
    },
    // 上传图片
    async uploadImg(files) {
      try{
        let file = await this.$api.files.uploadImg({
          filePath: files
        })
        return Promise.resolve(file.baseUrl + file.url)
      }catch(e){
        return Promise.reject(e)
      }
    },
    // 拨打电话
    onClickCallPhone() {
      uni.makePhoneCall({
        phoneNumber: this.res.storePhone
      })
    },
    // 选择发票
    onClickInvoiceImg() {
      if((this.res.status === 2 || this.res.status === 7) && !this.res.orderImgUrl) {
        uni.chooseImage({
          count: 1,
          success: res => {
            this.invoiceImg = res.tempFilePaths[0]
          }
        })
      }
    },
    // 上传发票
    async onClickUploadInvoice() {
      try{
        if(!this.invoiceImg) {
          uni.showToast({
            icon: 'none',
            title: '请选择发票'
          })
          return false
        }
        uni.showLoading({
          title: '提交中'
        })
        let res = await this.uploadImg(this.invoiceImg)
        let result = await this.$api.order.uploadInvoice({
          orderId: this.orderId,
          invoiceUrl: res
        })
        if(result.code === 200) {
          this.res.orderImgUrl = this.invoiceImg
          this.getShareOrderDetailInfo(this.orderId)
          uni.showToast({
					  icon: 'none',
					  title: '上传成功'
          })
        }else{
          uni.showToast({
					  icon: 'none',
					  title: result.msg
          })
        }
        uni.hideLoading()

      }catch(e){
        uni.hideLoading()
      }
    },
    // 服务商选择图片
    onClickInvoiceFormImg(type) {
      if(this.res.status !== 7) return false
      uni.chooseImage({
        count: 1,
        success: res => {
          this.invoiceForm[type] = res.tempFilePaths[0]
        }
      })
    },
    // 点击提交资料
    async onSubmitInfo() {
      let params = JSON.parse(JSON.stringify(this.invoiceForm))
      let toast = null
      if(!params.clientDrivingImg && !params.clientInsuranceImg && !params.clientInvoiceImg) {
        toast = '请上传客户发票'
      }
      if(toast) {
        uni.showToast({
          icon: 'none',
          title: toast
        })
        return false
      }
      uni.showLoading({
        title: '提交中'
      })
      try{
        let imgs = []
        let clientDrivingImg = ''
        let clientInsuranceImg = ''
        let clientInvoiceImg = ''

        if(params.clientDrivingImg) {
          clientDrivingImg = await this.uploadImg(params.clientDrivingImg)
          imgs.push({
            type: 0,
            imgUrl: clientDrivingImg
          })
        }
        if(params.clientInsuranceImg) {
          clientInsuranceImg = await this.uploadImg(params.clientInsuranceImg)
          imgs.push({
            type: 1,
            imgUrl: clientInsuranceImg
          })
        }
        if(params.clientInvoiceImg) {
          clientInvoiceImg = await this.uploadImg(params.clientInvoiceImg)
          imgs.push({
            type: 2,
            imgUrl: clientInvoiceImg
          })
        }

        let result = await this.$api.order.ServiceUploadInvoice({
          orderId: params.orderId,
          imgs: imgs
        })
        if(result.code === 200) {
          // this.res.orderImgUrl = this.invoiceImg
          this.getShareOrderDetailInfo(this.orderId)
          uni.showToast({
					  icon: 'none',
					  title: '上传成功'
          })
        }else{
          uni.showToast({
					  icon: 'none',
					  title: result.msg
          })
        }
        uni.hideLoading()

      }catch(e){
        uni.hideLoading()
      }
    },

    imclick(types) {
      console.log(types.carImg,'123')
      this.$store.commit('news/createConversationActive', {
        user: types.storeName, //店铺名称
        img: types.storeImg, //店铺头像
        lastTime: '', //
        userId: types.storeId, //店铺id
        conversationID: 'C2C' + types.storeId,
      })
      this.$store.commit('news/setCardInfo', {
			  imgSrc: types.carImg,
			  name: types.carName,
			  type: 'resaleCar',
			  price: types.factoryPrice,
        goPage: '3',
        payment: types.payMentMoney,//首付
        monthly: types.monthMoney//月供
      })
      uni.navigateTo({
        url: '/pages_common/contactSeller/contactSeller?user=' + types.storeName + '&pageId=' + types.orderId+ '&tab=1'+'&userId'+this.userId
      })
    },
    onJumpUser() {
      uni.navigateTo({
        url: '/pages_userModule/memberDetails/memberDetails?from=chat&id='+this.userId
      })
    },
    // 跳转店铺详情
    onJumpStore() {
      uni.navigateTo({
        url: `/pages_store/newCarStore/index/index?storeid=${this.res.storeId}`
      })
    }
  },
  onNavigationBarButtonTap(e) {
    uni.makePhoneCall({
      phoneNumber: this.$store.state.serviceNumber.number
    })
  }
}